package com.novel.controller.system;

import com.novel.common.enums.BeAuthorStatusEnum;
import com.novel.common.exception.CustomerException;
import com.novel.controller.other.BaseController;
import com.novel.pojo.system.ApplyAuthor;
import com.novel.pojo.system.User;
import com.novel.service.ApplyAuthorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

/**
 * 申诉相关内容全部在这里
 */
@Slf4j
@Controller
@RequestMapping("apply")
public class ApplyController extends BaseController {

    @Autowired
    private ApplyAuthorService applyAuthorService;

    /**
     * 申请成为作者
     */
    @PostMapping("author")
    @ResponseBody
    public boolean applyToAuthor(String reason) {
        User user = getUserInformation();

        if (user == null) {
            throw new CustomerException("用户未登录");
        }

        ApplyAuthor applyAuthor = ApplyAuthor.builder()
                .createdIp(getUserIpAddress())
                .createdTime(new Date())
                .id(UUID.randomUUID().toString())
                .reason(reason)
                .status(BeAuthorStatusEnum.WAIT.getCode())
                .updatedTime(new Date())
                .updatedIp(getUserIpAddress())
                .updatedId(user.getUserId())
                .userId(user.getUserId())
                .userEmail(user.getUserEmail())
                .build();

        return applyAuthorService.insertApplyAuthorInfo(applyAuthor);
    }


    /**
     * 取消申请
     */
    @PostMapping("author/cancel/{id}")
    @ResponseBody
    public boolean cancelAuthor(@PathVariable("id") String id) {
        User user = getUserInformation();

        if (user == null) {
            throw new CustomerException("用户未登录");
        }

        return applyAuthorService.cancelApplyAuthor(user.getUserId(), id, getUserIpAddress());
    }


}

